package com.snail.readyvr.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.snail.readyvr.R;

import butterknife.BindView;

/**
 * Created by MRKING on 2017/3/15.
 */

public abstract class BaseWebViewActivity extends CheckPermissionsActivity {


    @BindView(R.id.webview)
    WebView mWebView;
    @Nullable
    @BindView(R.id.webview_progressbar)
    ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWebView();
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("SetJavaScriptEnabled")
    protected void initWebView() {
        if (mWebView == null) {
            Log.e(TAG, "error status-->webviewe is null");
            return;
        }
        WebSettings set = mWebView.getSettings();
        set.setJavaScriptEnabled(true);
        set.setGeolocationEnabled(true);
        set.setJavaScriptCanOpenWindowsAutomatically(true);
        set.setSupportMultipleWindows(true);
        set.setSavePassword(true);
        set.setAllowContentAccess(true);
        set.setAllowFileAccess(true);
        set.setCacheMode(WebSettings.LOAD_NO_CACHE);
        set.setDomStorageEnabled(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                if (url.startsWith("http")) {
                    view.loadUrl(url);
                    return true;
                } else {
                    return false;
                }
            }

            @Override
            public void onPageFinished(WebView view, String url) {

                if (mProgressBar != null)
                    mProgressBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mProgressBar != null)
                    mProgressBar.setVisibility(View.VISIBLE);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub

                if (mProgressBar != null) {
                    if (newProgress == 100) {
                        // 网页加载完成
                        mProgressBar.setVisibility(View.GONE);
                    } else {
                        // 加载中
                        mProgressBar.setProgress(newProgress);
                    }
                }
            }
        });
    }

    protected void reLoad() {
        if (mWebView != null) {
            mWebView.reload();
        }
    }

    protected void loadUrl(String url) {
        if (mWebView != null) {
            mWebView.loadUrl(url);
        }
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null) {
            mWebView.stopLoading();
            mWebView.loadData("", "text/html", "utf-8");
        }
        super.onDestroy();
    }

    // 改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(mWebView != null && mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}
